// Comprehensive Reference and Value Type Conversion Test
// Tests value-to-reference and reference-to-value conversions for all types

type Person struct {
    name String
    age int
}

func test_reference_suite() {
    println("========================================")
    println("Reference Conversion Test Suite")
    println("========================================")
    println("")

    // =====================================================
    // Test 1: int reference conversion
    // =====================================================
    println("[Test 1] int value to reference")
    var x int = 100
    var ref_x &int = &x
    assert(ref_x == 100, "ref_x should equal x (100)")
    println("  PASS: Value to reference conversion")

    var y int = ref_x
    assert(y == 100, "y should equal ref_x (100)")
    println("  PASS: Reference to value conversion")

    ref_x = 200
    assert(x == 200, "x should be 200 after ref_x = 200")
    assert(ref_x == 200, "ref_x should be 200")
    println("  PASS: Direct reference assignment")
    println("")

    // =====================================================
    // Test 2: float64 reference conversion
    // =====================================================
    println("[Test 2] float64 value to reference")
    var f float64 = 3.14
    var ref_f &float64 = &f
    assert(ref_f == 3.14, "ref_f should equal f (3.14)")
    println("  PASS: float64 reference conversion")

    ref_f = 2.71
    assert(f == 2.71, "f should be 2.71 after modification")
    println("  PASS: Modified float64 through reference")
    println("")

    // =====================================================
    // Test 3: bool reference conversion
    // =====================================================
    println("[Test 3] bool value to reference")
    var flag bool = true
    var ref_flag &bool = &flag
    assert(ref_flag == true, "ref_flag should be true")
    println("  PASS: bool reference conversion")

    ref_flag = false
    assert(flag == false, "flag should be false after ref_flag = false")
    println("  PASS: Modified bool through reference")
    println("")

    // =====================================================
    // Test 4: Multiple references to same value
    // =====================================================
    println("[Test 4] Multiple references sharing value")
    var value int = 42
    var ref1 &int = &value
    var ref2 &int = &value

    assert(ref1 == 42, "ref1 should be 42")
    assert(ref2 == 42, "ref2 should be 42")
    println("  PASS: Both references point to same value")

    ref1 = 100
    assert(value == 100, "value should be 100")
    assert(ref2 == 100, "ref2 should also be 100")
    println("  PASS: Modification through ref1 affects value and ref2")
    println("")

    // =====================================================
    // Test 5: Pointer reassignment
    // =====================================================
    println("[Test 5] Pointer reassignment")
    var a int = 10
    var b int = 20
    var ref_a &int = &a

    assert(ref_a == 10, "ref_a should be 10")
    println("  PASS: ref_a initially points to a")

    // ref_a = &b
    // assert(ref_a == 20, "ref_a should be 20 after reassignment")
    println("  SKIP: Reference reassignment (not yet implemented)")

    ref_a = 30
    assert(a == 30, "a should be 30 after ref_a = 30")
    assert(b == 20, "b should still be 20")
    println("  PASS: Modification affects a, not b")
    println("")

    // =====================================================
    // Test 6: Pointer in expressions
    // =====================================================
    println("[Test 6] Pointers in expressions")
    var num1 int = 5
    var num2 int = 10
    var ref_num1 &int = &num1
    var ref_num2 &int = &num2

    var sum int = ref_num1 + ref_num2
    assert(sum == 15, "sum should be 15")
    println("  PASS: Arithmetic with dereferenced references")

    var is_equal bool = ref_num1 == 5
    assert(is_equal, "ref_num1 should equal 5")
    println("  PASS: Comref_arison with dereferenced references")
    println("")

    // =====================================================
    // Summary
    // =====================================================
    println("=========================================")
    println("ALL TESTS PASSED!")
    println("=========================================")
    println("")
    println("Conversion Summary:")
    println("  Value -> Reference: var ref &T = &value")
    println("  Reference -> Value: var v T = reference")
    println("  Reference sharing: Multiple references can reference same variable")
    println("  All basic types supported: int, float64, bool, etc.")
    println("")

    // =====================================================
    // Additional Comprehensive Tests
    // =====================================================
    println("========================================")
    println("Comprehensive Reference Dereference Test")
    println("========================================")
    
    // Test 1: int
    println("\nTest 1: &int")
    var temp1 int = 100
    var p1 &int = &temp1
    println("Read: ", p1)
    p1 = 200
    println("After write: ", p1)
    // defer free(p1)
    
    // Test 2: int64
    println("\nTest 2: &int64")
    var temp2 int64 = 1000000
    var p2 &int64 = &temp2
    println("Read: ", p2)
    p2 = 2000000
    println("After write: ", p2)
    // defer free(p2)
    
    // Test 3: float32
    println("\nTest 3: &float32")
    var temp3 float32 = 3.14
    var p3 &float32 = &temp3
    println("Read: ", p3)
    p3 = 2.71
    println("After write: ", p3)
    // defer free(p3)
    
    // Test 4: float64
    println("\nTest 4: &float64")
    var temp4 float64 = 3.14159265
    var p4 &float64 = &temp4
    println("Read: ", p4)
    p4 = 2.71828182
    println("After write: ", p4)
    // defer free(p4)
    
    // Test 5: bool
    println("\nTest 5: &bool")
    var temp5 bool = true
    var p5 &bool = &temp5
    println("Read: ", p5)
    p5 = false
    println("After write: ", p5)
    // defer free(p5)
    
    // Test 6: int8
    println("\nTest 6: &int8")
    var temp6 int8 = 127
    var p6 &int8 = &temp6
    println("Read: ", p6)
    p6 = -128
    println("After write: ", p6)
    // defer free(p6)
    
    // Test 7: uint
    println("\nTest 7: &uint")
    var temp7 uint = 4294967295
    var p7 &uint = &temp7
    println("Read: ", p7)
    p7 = 1234567
    println("After write: ", p7)
    // defer free(p7)
    
    println("\n=== All comprehensive tests completed successfully! ===")
    
    // =====================================================
    // Struct Reference Test
    // =====================================================
    println("\n========================================")
    println("Struct Reference Test")
    println("========================================")
    
    // Create a reference to struct
    var p &Person = &Person{name: "Alice", age: 30}
    
    println("Test 1: Reference access")
    println("Reference name: ", p.name, ", age: ", p.age)
    
    // Try to dereference to get value
    var v Person = p
    p.age = p.age + 1
    
    println("Test 2: Value after dereference")
    println("Value name: ", v.name, ", age: ", v.age)
    println("Reference name: ", p.name, ", age: ", p.age)
    
    println("\n=== All struct reference tests completed successfully! ===")
}
